//
//  ParkingRecordListController.m
//  HuLaQuan
//
//  Created by hok on 3/16/16.
//  Copyright © 2016 yuwubao. All rights reserved.
//

#import "ParkingRecordListController.h"

//cells
#import "ParkingRecordsCell.h"

//logic model
#import "ParkingRecordsEngine.h"

@interface ParkingRecordListController ()

@property (strong, nonatomic) NSMutableArray *parkingRecordList; //of Records;

@property (strong, nonatomic) NSIndexPath *selectIndexPath;

@property(nonatomic,strong) ParkingRecordsEngine *listEngine;
@end

static NSString * const ParkingRecordsCellIdentifier = @"ParkingRecordsCell";

@implementation ParkingRecordListController

- (ParkingRecordsEngine *)listEngine
{
    if (!_listEngine) {
        _listEngine = [[ParkingRecordsEngine alloc] init];
    }
    return _listEngine;
}

- (NSMutableArray *)parkingRecordList
{
    if (!_parkingRecordList) {
        _parkingRecordList = [[NSMutableArray alloc] init];
    }
    return _parkingRecordList;
}

#pragma mark -  Life Cycle

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getParkingRecordsFinish:) name:@"GetParkingRecordsFinish" object:nil];
    
    [self testData];
   // [self refreshTableView];
    
}
- (void)testData
{
    [self.parkingRecordList addObject:@"haha"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GetParkingRecordsFinish" object:nil];
    
}

- (void)setupUI
{
    
    [self setupFooterForTableView];
    
    [self setupNavBackButton];
    
    [self.tableView registerNib:[ParkingRecordsCell nib] forCellReuseIdentifier:ParkingRecordsCellIdentifier];
    
    self.title = @"停车记录";
//    
//    [self addRefreshView];
//    
//    [self refreshTableView];
    
    
}
- (void)refreshTableView
{
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    [self.listEngine getParkingRecordsOfCar:self.carInfoAndTime[@""] estate:self.carInfoAndTime[@""] startTime:self.carInfoAndTime[@""] endTime:self.carInfoAndTime[@""]];
}
#pragma mark -  Call Back
- (void)getParkingRecordsFinish:(NSNotification*) notification
{
    //
    //   dispatch_async(dispatch_get_main_queue(), ^{
    [YuNetworkStatusUtilities dismissHUD];
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue]) {
        
    
            if ([messageDic[@"result"]isKindOfClass:[NSArray class]]) {
    
    
    
                [self.parkingRecordList addObjectsFromArray:messageDic[@"result"]];
    
                [self.tableView reloadData];
            }
        
        NSLog(@"%@",messageDic[@"result"]);
//        if ([messageDic[@"result"][@"list"][@"trade"] isKindOfClass:[NSArray class]]) {
//            
//            
//            if (self.isPull) {
//                [self.paymentList removeAllObjects];
//            }
//            
//            [self.paymentList addObjectsFromArray:messageDic[@"result"][@"list"][@"trade"] ];
//            
//            [self.tableView reloadData];
//        }
        
        
        
    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
    
    /*if (self.isPull) {
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }else{
        [self.tableView footerEndRefreshing];
    }*/
    
    //  });
    
}
#pragma mark -  UITableViewDataSource



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kParkingRecordsCellHeight;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.parkingRecordList.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return kUserCenterSectionHeaderHeight;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ParkingRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:ParkingRecordsCellIdentifier
                                                            forIndexPath:indexPath];
//    // cell.noticeLabel.text = @"请添加新账户";
//    
//    // cell.titleLabel.text = self.parkingRecordList[indexPath.row][@"title"];
//    cell.detailLabel.text = self.parkingRecordList[indexPath.row][@"resName"];
//    cell.timeLabel.text = [self.parkingRecordList[indexPath.row][@"txnDate"] substringToIndex:10];
//    cell.titleLabel.text = ServerMoneyFloatToShow(self.parkingRecordList[indexPath.row][@"txnAmt"]) ;
    
    return cell;
    
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
